//
//  AnnouncementViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/5/18.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "AnnouncementViewController.h"
#import "AnnouncementCell.h"
#import "AnnouncementModel.h"

@interface AnnouncementViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_announcementArray;
}
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation AnnouncementViewController

- (void)AnnouncementMsgRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"page":@"1"
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kAnnouncement];
    [self showHUDInView:self.view title:nil];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        [self hideHUD];
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *dic in array) {
                AnnouncementModel *model = [[AnnouncementModel alloc] initWithDict:dic];
                [_announcementArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        [self hideHUD];
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self AnnouncementMsgRequest];
    [self setUpUI];
    self.navigationItem.title = @"公告消息";
}

#pragma mark -- UI
- (void)setUpUI {
    _announcementArray = [NSMutableArray arrayWithCapacity:1];
    //tableView
    [self creatTableView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _announcementArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AnnouncementCell *cell = [AnnouncementCell loadNibCellWithTabelView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AnnouncementModel *model = _announcementArray[indexPath.row];
    cell.announcementModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AnnouncementModel *model = _announcementArray[indexPath.row];
    CGSize maxSize = CGSizeMake(kSCREEN_WIDTH - 30, MAXFLOAT);
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: [UIColor colorWithWhite:0.263 alpha:1.000]};
    CGSize textSize = [model.b_description boundingRectWithSize:maxSize
                                                         options:NSStringDrawingUsesLineFragmentOrigin
                                                      attributes:attributes
                                                         context:nil].size;
    CGSize richTextSize = CGSizeMake(textSize.width, textSize.height + 2);
    return richTextSize.height + 45;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
